package com.qdlc.p2p.biz.web.filter;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.qdlc.p2p.biz.util.UserLoginUtil;
import com.qdlc.p2p.common.constant.Constant;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.common.SystemConfigHelper.Nid;
import com.qdlc.p2p.dal.dto.User;

/**
 * 用于session过滤，补充Struts2的session拦截器
 * 
 * @author fuxingxing
 */
public class SessionFilter implements Filter {
	protected String encoding = null;

	protected boolean ignore = false;

	public void destroy() {
		this.encoding = null;
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
		ServletException {

		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
		HttpServletResponse httpServletResponse = (HttpServletResponse) response;
		// 通过检查session中的变量，过虑请求
		User sessionUser = UserLoginUtil.getLoginUser(httpServletRequest);
		// 当前会话用户为空而且不是请求登录，退出登录，欢迎页面和根目录则退回到应用的根目录
		String servletPath = httpServletRequest.getServletPath();
		String queryString = httpServletRequest.getQueryString();
		String web_url = SystemConfigHelper.getValue(Nid.WEB_URL);
		List<String> pathList = notNeedSessionCheck();
		if (!pathList.contains(servletPath)) {
			if (sessionUser == null) {
				String redirectURL ="";
				List<String> rectPathList = notRedirectURLCheck();
				//如果包含要过滤的路径，则跳转到个人中心
				if (rectPathList.contains(servletPath)) {
					redirectURL = "/member/main.html";
				}else{
					redirectURL=servletPath;
				}
				
				if (StringUtil.isNotBlank(queryString)) {
					redirectURL = httpServletRequest.getContextPath() + servletPath + "?" + StringUtil.isNull(queryString);
				}
				redirectURL = java.net.URLEncoder.encode(redirectURL, "UTF-8");
				httpServletResponse.sendRedirect(web_url + "user/login.html?timeout=1&redirectURL=" + redirectURL);
				return;
			}
		}

		chain.doFilter(request, response);
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		this.encoding = filterConfig.getInitParameter("encoding");
		String value = filterConfig.getInitParameter("ignore");
		if(value == null || value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes")){
			this.ignore = true;
		}else{
			this.ignore = false;
		}
	}

	protected String selectEncoding(ServletRequest request) {
		return (this.encoding);
	}

	private List<String> notNeedSessionCheck() {
		String[] paths = new String[] { "/member/identify/active.html", "/member/pay/gopay.html",
				"/member/identify/active.action", "/member/pay/gopay.action","/member/recharge/doRecharge.html"};

		return Arrays.asList(paths);
	}
	
	private List<String> notRedirectURLCheck(){
		String[] noRedirect = new String[]{"/invest/tender.html"};
		return Arrays.asList(noRedirect);
	}
}
